# Maintainer: Miloš Komarčević <miloskomarcevic@aim.com>

_realname=libaec
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.0.4
pkgrel=1
pkgdesc="Adaptive Entropy Coding library (mingw-w64)"
arch=('any')
url="https://gitlab.dkrz.de/k202009/libaec"
license=('BSD-2-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-crt")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake")
options=('staticlibs' 'strip')
_md5=ea0b7d197a950b0c110da8dfdecbb71f
source=("${url}/uploads/${_md5}/${_realname}-${pkgver}.tar.gz"
        "0001-fix-static-build.patch"::"${url}/-/commit/0c7c88e63771d7d7286b5dd7d027160140df012f.patch"
        "0002-build-tests-off.patch"
        "0003-build-szip-remove.patch")
sha256sums=('f2b1b232083bd8beaf8a54a024225de3dd72a673a9bcdf8c3ba96c39483f4309'
            'b1f470b23a50f21e96b836b1dd6e5b03c3ef0246653b8e3531ac59d838502a22'
            '5673b920e057f657e1a3cd6fc693b31feeda1ea4c5d871ffd60939a336053522'
            'abedcc750e2742cfdc2bfefc09d0d37db06e12f58ee136da0eb8905ea8febd1e')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/0001-fix-static-build.patch
  patch -p1 -i ${srcdir}/0002-build-tests-off.patch
  patch -p1 -i ${srcdir}/0003-build-szip-remove.patch
}

build() {
  [[ -d ${srcdir}/build-static-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-static-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-static-${MINGW_CHOST} && cd ${srcdir}/build-static-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED_LIBS=OFF \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .

  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd ${srcdir}/build-static-${MINGW_CHOST}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build ./ --target install
  cd ${srcdir}/build-${MINGW_CHOST}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build ./ --target install
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/Copyright.txt ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/Copyright.txt
}
